package cc.yugu.yhdw.common.domain.basic;

import cc.yugu.yhdw.common.annotation.Transient;
import cc.yugu.yhdw.common.domain.LongIdEntity;
import cc.yugu.yhdw.common.entity.json.DateTimeSerializer;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 短息发送记录实体类
 * 
 * @Date:Created in 2018/7/25 11:11
 * @Author:
 * @Modified By: zjn 2018/7/25 11:11
 */ 
public class MobileMessage extends LongIdEntity {

    public static final int DELAY_TIME_ZERO = 0;
    public static final int DELAY_THIRTY_SECOND = 30;

    public static final int MAX_RESEND_NUM = 1;

    public enum MessageStatus {
        NOT(1, "未发"),
        OK(2, "成功"),
        FAIL(3, "失败"),
        ;

        private final int value;
        private final String name;

        private MessageStatus(int value, String name) {
            this.value = value;
            this.name = name;
        }

        private static Map<Integer, String> map = new HashMap<Integer, String>();
        static {
            for (MessageStatus e : MessageStatus.values()) {
                map.put(e.getValue(), e.getName());
            }
        }

        public static String getName(int value) {
            return map.get(value);
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }

    public enum ClwCallbackStatus {

        DELIVRD("DELIVRD", "短消息转发成功"),
        EXPIRED("EXPIRED","短消息超过有效期"),
        UNDELIV("UNDELIV", "短消息是不可达的"),
        UNKNOWN("UNKNOWN", "未知短消息状态"),
        REJECTD("REJECTD", "短消息被短信中心拒绝"),
        DTBLACK("DTBLACK", "目的号码是黑名单号码"),
        ERR_104("ERR:104", "系统忙"),
        REJECT("REJECT", "审核驳回"),
        other("其他", "网关内部状态");

        private final String value;
        private final String name;

        private ClwCallbackStatus(String value, String name) {
            this.value = value;
            this.name = name;
        }

        private static Map<String, String> map = new HashMap<String, String>();
        static {
            for (ClwCallbackStatus e : ClwCallbackStatus.values()) {
                map.put(e.getValue(), e.getName());
            }
        }

        public static String getName(String value) {
            return map.get(value);
        }

        public String getValue() {
            return value;
        }

        public String getName() {
            return name;
        }

    }

    public enum SourceType {
        DEPOSIT(5, "充值"),
        AUTH_CODE(6, "验证码")
        ;

        private final int value;
        private final String name;

        private SourceType(int value, String name) {
            this.value = value;
            this.name = name;
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }

        public static String getSourceTypeName(int value) {
            String rtn = null;
            for (SourceType e : SourceType.values()) {
                if (e.getValue() == value) {
                    rtn = e.getName();
                    break;
                }
            }
            return rtn;
        }
    }
    /**
    * 返回拼接后的 验证码变量 （如：{"authCode":"067627"}  ）
    * 
    * @Param: [variable]
    * @Return: java.lang.String
    * @Author By: zjn 2018/7/25  13:24
    */ 
    public static String buildVariableJson(String[] variable) {
        // 创几十年一个一个字符串生成器对象，其中没有字符，初始容量为16个字符。
        StringBuilder builder = new StringBuilder();
        // 如果验证码长度不是偶数位
        if(variable.length % 2 != 0) {
            // 抛出此异常，表明向方法传递了一个不合法或不正确的参数。
            throw new IllegalArgumentException();
        }
        // 拼接
        builder.append("{");
        for(int i = 0; i < variable.length; i = i + 2) {
            // 往容器中添加 "  ， 双引号前要加转义字符：\
            builder.append("\"");
            builder.append(variable[i]);
            builder.append("\":");
            builder.append("\"");
            builder.append(variable[i + 1]);
            builder.append("\"");
            // 如果有两组验证码
            if(i < variable.length - 2) {
                builder.append(",");
            }
        }
        builder.append("}");
        return builder.toString();
    }

    Integer agentId;
    String sourceId;
    Integer sourceType;
    String content;
    String mobile;
    String variable; //变量
    String templateCode; //编码
    Date handleTime;
    Date createTime;
    Integer status;
    Integer senderId;
    Integer type;
    Integer delay;
    String msgId;
    Integer resendNum;
    String callbackStatus;

    @Transient
    String senderName;

    public Integer getAgentId() {
        return agentId;
    }

    public void setAgentId(Integer agentId) {
        this.agentId = agentId;
    }

    public String getSourceId() {
        return sourceId;
    }

    public void setSourceId(String sourceId) {
        this.sourceId = sourceId;
    }

    public Integer getSourceType() {
        return sourceType;
    }

    public void setSourceType(Integer sourceType) {
        this.sourceType = sourceType;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getVariable() {
        return variable;
    }

    public void setVariable(String variable) {
        this.variable = variable;
    }

    public String getTemplateCode() {
        return templateCode;
    }

    public void setTemplateCode(String templateCode) {
        this.templateCode = templateCode;
    }

    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getHandleTime() {
        return handleTime;
    }

    public void setHandleTime(Date handleTime) {
        this.handleTime = handleTime;
    }

    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getSenderId() {
        return senderId;
    }

    public void setSenderId(Integer senderId) {
        this.senderId = senderId;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public Integer getDelay() {
        return delay;
    }

    public void setDelay(Integer delay) {
        this.delay = delay;
    }

    public String getStatusName() {
        if(status != null) {
            return MessageStatus.getName(status);
        }
        return "";
    }

    public String getSenderName() {
        return senderName;
    }

    public void setSenderName(String senderName) {
        this.senderName = senderName;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public Integer getResendNum() {
        return resendNum;
    }

    public void setResendNum(Integer resendNum) {
        this.resendNum = resendNum;
    }

    public String getCallbackStatus() {
        return callbackStatus;
    }

    public void setCallbackStatus(String callbackStatus) {
        this.callbackStatus = callbackStatus;
    }

}
